/**
 * 产品属性特征值维护 - 编辑页面
 * @author shixc
 * @date 2018/01/11
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        current = 0,
        vueData = {
            count: 0,
            isAdd: false,
            imgs: g_ctxPath + 'crm/gwai500/imgs',
            files: g_ctxPath + 'upload/files',
            uploadUrl: g_ctxPath + 'upload/files',
            address: '',
            img: '',
            status: true,
            detailReadonly: false,
            gwam005: true,
            form: {
                GWAM007_NAME: null,
                GWAM004_NAME: null,
                GWAM008_NAME: null
            },
            videoForm: {
                videoUploadId: null,
                Video: null
            },
            videoFlag:false,
            videoUploadPercent:0,
            file: '',
            fileList: [],
            imgList: [],
            imgList2: [],
            videoList: [],
            dialogImageUrl: '',
            dialogVisible: false,
            activeName2: 'first',
            rules: {
                GWAM001: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWAM002: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWAML003: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWAML004: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWAML005: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWAM004_NAME: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWAM007_NAME: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWAM008: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWAM009: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWAM027: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWAM028: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWAM030: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWAMACTI: [
                    {required: true, message: '不可为空', trigger: 'select'}
                ]
            },
            selectOptions: {
                sys_y_n: app.datas_("sys_y_n"),
                gwam027: app.datas_("gwam027"),
                gwam005: app.datas_("gwam005"),
                crm_gwai500_gwam028: app.datas_("crm_gwai500_gwam028"),
                gwam030: app.datas_("crm_gwai500_gwam030")
            }
        },
        callObj,
        layOpenIndex,
        pageVue
    ;

    var Gwai500Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai500_edit',
                _loadTplsFull: [{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                },
                    {
                        dom: 'gwai500_img', tpl: 'gwai500_img'
                    }]
            }, win);
        },
        edit: function () {
            app.ajax({
                xurl: 'crm/gwai500/editGwam',
                xdata: vueData.form,
                loading: true,
                okmsg: '产品资料信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        callObj.update(result.xdata);
                        vueData.fileList = [];
                        vueData.videoList = [];
                        vueData.imgList = [];
                        vueData.img = '';
                        vueData.address = '';
                        //app.pageModule('gwai500').reloadTableCurr();
                        layer.close(layOpenIndex);
                    }
                }
            });
        },
        add: function () {
            app.ajax({
                xurl: 'crm/gwai500/addGwam',
                xdata: vueData.form,
                loading: true,
                okmsg: '产品资料信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        vueData.fileList = [];
                        vueData.videoList = [];
                        vueData.imgList = [];
                        vueData.img = '';
                        vueData.address = '';
                        app.pageModule('gwai500').reloadTableCurr();
                        layer.close(layOpenIndex);
                    }
                }
            });
        },
        _call: function (obj) {
            pageVue.$refs['form'].resetFields();
            layOpenIndex = obj.layOpenIndex;
            callObj = obj.obj;
            vueData.fileList = [];
            vueData.videoList = [];
            vueData.imgList2 = [];
            vueData.imgList = [];
            vueData.img = '';
            vueData.address = '';
            vueData.gwam005 = true;
            vueData.count = 0;
            if (obj.is_add) {
                vueData.isAdd = true;
                vueData.detailReadonly = false;
                app.extendUpLow({
                    target: vueData.form,
                    res: {
                        GWAJACTI: 'Y',
                        GWAMACTI: 'Y',
                        GWAM005: '0'
                    },
                    reset: true
                });
            } else {
                vueData.isAdd = false;
                if (obj.operation == 'detail') {
                    vueData.detailReadonly = true;
                } else {
                    vueData.detailReadonly = false;
                }
                if (callObj.data.GWAM015 != undefined && callObj.data.GWAM015 != "") {
                    if (callObj.data.GWAM015.length > 0) {
                        vueData.img = callObj.data.GWAM015;
                        vueData.status = false;
                        var list = callObj.data.GWAM015.split(',');
                        for (var i = 0; i < list.length; i++) {
                            vueData.imgList.push({
                                name: list[i], url: '/wissun-smfile/' + list[i]
                            })
                            vueData.imgList2.push({GWAM015: list[i]});
                        }
                        /*vueData.imgList.push({
                            name: callObj.data.GWAM015, url: '/wissun-smfile/' + callObj.data.GWAM015
                        })*/
                    }
                }
                if (callObj.data.GWAM018 != undefined && callObj.data.GWAM018 != "") {
                    if (callObj.data.GWAM018.length > 0) {
                        vueData.address = callObj.data.GWAM018;
                        vueData.fileList.push({
                            name: callObj.data.GWAM018, url: '/wissun-smfile/' + callObj.data.GWAM018
                        })
                    }
                }
                if (callObj.data.GWAM031 != undefined  && callObj.data.GWAM018 != "") {
                    if (callObj.data.GWAM031.length > 0) {
                        vueData.videoForm.Video = '/wissun-smfile/' + callObj.data.GWAM031;
                        vueData.videoList.push({
                            name: callObj.data.GWAM031, url: '/wissun-smfile/' + callObj.data.GWAM031
                        })
                    }
                }
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
                if(vueData.form.GWAM005 == '0'){
                    vueData.gwam005 = true;
                }else{
                    vueData.gwam005 = false;
                }
            }

        },
        _ready: function () {
            vueData.form = app.fields_(['GWAM'], vueData.form);
            pageVue = new Vue({
                el: '#gwai500_edit',
                data: vueData,
                watch: {
                    'form.GWAM008': function (data) {
                        vueData.form.GWAM009 = data;
                    },
                    'form.GWAM008_NAME': function (data) {
                        if (vueData.form.GWAM008_NAME == '') {
                            vueData.form.GWAM008 = '';
                        }
                    }
                },
                methods: {
                    cancel: function () {
                        layer.close(layOpenIndex);
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.form.GWAML005 != null && vueData.form.GWAML005 != '') {
                                    if (vueData.form.GWAML005.length > 30) {
                                        app.showWarning('助记码长度不可以大于30!');
                                        return;
                                    }
                                }
                                if (vueData.isAdd) {
                                    if(vueData.form.GWAM006 == '' || vueData.form.GWAM006 == null){
                                        vueData.form.GWAM006 = vueData.form.GWAM001;
                                    }
                                    var flag = true;
                                    if (pageVue.$refs.upload2.uploadFiles.length > 0) {
                                        flag = false;
                                        pageVue.$refs.upload2.submit();
                                    }else if (pageVue.$refs.upload3.uploadFiles.length > 0) {
                                        flag = false;
                                        pageVue.$refs.upload3.submit();
                                    } else {
                                        if (pageVue.$refs.upload1.uploadFiles.length > 0) {
                                            flag = false;
                                            pageVue.$refs.upload1.submit();
                                        }
                                    }
                                    if (flag) {
                                        self.add();
                                    }
                                } else {
                                    vueData.form.GWAM015 = '';
                                    for (var i = 0; i < vueData.imgList2.length; i++) {
                                        if (i == 0) {
                                            vueData.form.GWAM015 = vueData.imgList2[i].GWAM015;
                                        } else {
                                            vueData.form.GWAM015 = vueData.form.GWAM015 + ',' + vueData.imgList2[i].GWAM015;
                                        }
                                    }

                                    vueData.imgList2.forEach(function (v) {
                                        pageVue.$refs.upload1.uploadFiles.removeByMap({
                                            name: v.GWAM015
                                        });
                                    });
                                    pageVue.$refs.upload2.uploadFiles.removeByMap({
                                        name: vueData.address
                                    });
                                    pageVue.$refs.upload3.uploadFiles.removeByMap({
                                        name: vueData.form.GWAM031
                                    });
                                    var flag = true;
                                    if (pageVue.$refs.upload2.uploadFiles.length > 0) {
                                        flag = false;
                                        pageVue.$refs.upload2.submit();
                                    }else if (pageVue.$refs.upload3.uploadFiles.length > 0) {
                                        flag = false;
                                        pageVue.$refs.upload3.submit();
                                    } else {
                                        if (pageVue.$refs.upload1.uploadFiles.length > 0) {
                                            flag = false;
                                            pageVue.$refs.upload1.submit();
                                        }
                                    }
                                    if (flag) {
                                        self.edit();
                                    }
                                }
                            }
                        });
                    },
                    openGwam019: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwas001',
                                    columnList: [{label: '仓库编号', prop: 'GWAS001', width: '150'},
                                        {label: '仓库名称', prop: 'GWASL003', width: '150'},
                                        {label: '助记码', prop: 'GWASL004', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.GWAM019_NAME = data.GWASL003;
                                    vueData.form.GWAM019 = data.GWAS001;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择仓库',
                                area: ['600px', '470px']
                            }
                        });
                    },
                    openGwam006: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'cq_gwam001_2',
                                    columnList: [{label: '产品编号', prop: 'GWAM001', width: '150'},
                                        {label: '品名', prop: 'GWAML003', width: '150'},
                                        {label: '规格', prop: 'GWAML004', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.GWAM006 = data.GWAM001;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择产品',
                                area: ['600px', '470px']
                            }
                        });
                    },
                    openGwam007: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwah001',
                                    columnList: [{label: '产品分类编号', prop: 'GWAH001', width: '150'},
                                        {label: '说明', prop: 'GWAHL003', width: '150'},
                                        {label: '助记码', prop: 'GWAHL004', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.GWAM007_NAME = data.GWAHL003;
                                    vueData.form.GWAM007 = data.GWAH001;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择产品分类',
                                area: ['600px', '470px']
                            }
                        });
                    },
                    openGwam004: function () {
                        if(vueData.form.GWAM007_NAME === null || vueData.form.GWAM007_NAME === ""){
                            app.showWarning("请先选择产品分类");
                            return;
                        }
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwao001',
                                    searchParam: {
                                        GWAO002: vueData.form.GWAM007
                                    },
                                    columnList: [
                                        { label: '产品系列编号', prop: 'GWAO001', width: '150'},
                                        { label: '说明', prop: 'GWAOL003', width: '150'},
                                        { label: '助记码', prop: 'GWAOL004', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.GWAM004_NAME = data.GWAOL003;
                                    vueData.form.GWAM004 = data.GWAO001;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择产品系列',
                                area: ['600px', '470px']
                            }
                        });
                    },
                    openGwam008: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwab001',
                                    columnList: [{label: '单位编号', prop: 'GWAB001', width: '150'},
                                        {label: '说明', prop: 'GWABL003', width: '150'},
                                        {label: '助记码', prop: 'GWABL004', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.GWAM008_NAME = data.GWABL003;
                                    vueData.form.GWAM008 = data.GWAB001;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择销售单位',
                                area: ['600px', '470px']
                            }
                        });
                    },
                    gwam004: function (val) {
                        if (val == '') {
                            vueData.form.GWAM004 = '';
                        }
                    },
                    gwam005Change: function (val) {
                        if (val == '0' || val == '') {
                            vueData.gwam005 = true;
                            vueData.form.GWAM006 = vueData.form.GWAM001;
                        }else{
                            vueData.gwam005 = false;
                        }
                    },
                    gwam007: function (val) {
                        if (val == '') {
                            vueData.form.GWAM007 = '';
                        }
                    },
                    gwam019: function (val) {
                        if (val == '') {
                            vueData.form.GWAM019 = '';
                        }
                    },
                    gwam021: function (val) {
                        if (val != '') {
                            if (!(/^[0-9]+.?[0-9]*$/).test(val)) {
                                val = '';
                                vueData.form.GWAM021 = '';
                                app.showError('发货体积需输入大于0的数字!');
                            }
                        }
                    },
                    gwam024: function (val) {
                        if (val != '') {
                            if (!(/^[0-9]+.?[0-9]*$/).test(val)) {
                                val = '';
                                vueData.form.GWAM024 = '';
                                app.showError('发货重量需输入大于0的数字!');
                            }
                        }
                    },
                    beforeUploadImg: function (file) {
                        const isJPG = (file.type === 'image/png' || file.type === 'image/jpeg'
                        || file.type === 'image/gif');
                        const isLt2M = file.size / 1024 / 1024 < 1;
                        if (!isJPG) {
                            this.$message.error("上传图片只能是 JPG|PNG|GIF 格式!");
                        }
                        if (!isLt2M) {
                            this.$message.error("上传图片大小不能超过 1MB!");
                        }

                        return isJPG && isLt2M;
                    },
                    successUploadImg: function (response) {
                        if (response.code == 200) {
                            pageVue.$message.success('图片上传成功');
                            if (vueData.form.GWAM015 == '' || vueData.form.GWAM015 == null) {
                                vueData.form.GWAM015 = response.data[0].name;
                            } else {
                                vueData.form.GWAM015 = vueData.form.GWAM015 + ',' + response.data[0].name;
                            }
                            vueData.count++;
                            if (vueData.count == pageVue.$refs.upload1.uploadFiles.length) {
                                if (vueData.isAdd) {
                                    self.add();
                                } else {
                                    self.edit();
                                }
                                pageVue.$refs.upload1.clearFiles();
                            }
                        }
                    },
                    beforeUploadFiles: function (file) {
                        const isJPG = (file.type === 'application/pdf'
                        || file.type === 'application/msword' || file.type === 'application/vnd.ms-excel'
                        || file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                        || file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/gif'
                        || file.type === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
                        const isLt2M = file.size / 1024 / 1024 < 5;
                        if (!isJPG) {
                            this.$message.error('文件格式不对');
                        }
                        if (!isLt2M) {
                            this.$message.error('文件大小不对');
                        }
                        return isJPG && isLt2M;
                    },
                    successUploadFiles: function (response) {
                        if (response.code == 200) {
                            pageVue.$message.success('文件上传成功');
                            vueData.form.GWAM018 = response.data[0].name;
                            if(pageVue.$refs.upload3.uploadFiles.length > 0){
                                pageVue.$refs.upload3.submit();
                            }else if (pageVue.$refs.upload1.uploadFiles.length > 0) {
                                pageVue.$refs.upload1.submit();
                            } else {
                                if (vueData.isAdd) {
                                    self.add();
                                } else {
                                    self.edit();
                                }
                                pageVue.$refs.upload2.clearFiles();
                            }
                        }
                    },
                    handleJpgRemove: function (file, fileList) {
                        if (!vueData.isAdd) {
                            vueData.imgList2.removeByMap({
                                GWAM015: file.name
                            });
                            file = '';
                        }
                    },
                    handleFileRemove: function (file, fileList) {
                        vueData.form.GWAM018 = "";
                        file = '';
                    },
                    handleVideoRemove: function (file, fileList) {
                        vueData.form.GWAM031 = "";
                        vueData.videoForm.Video = "";
                        file = '';
                    },
                    handleImgPreview: function (file) {
                        /*var img_infor = "<img src='" + '/wissun-smfile/' + file.name + "' />";
                        layer.open({
                            type: 1,
                            title: false,
                            closeBtn: 1,
                            area: '516px',
                            skin: 'layui-layer-nobg', //没有背景色
                            shadeClose: true,
                            content: img_infor
                        });*/
                        vueData.dialogImageUrl = '/wissun-smfile/' + file.name;
                        vueData.dialogVisible = true;
                    },
                    handleFilePreview: function (file) {
                        window.open('/wissun-smfile/' + file.name);
                    },
                    handleExceed: function (files, fileList) {
                        this.$message.warning("当前只允许选择一个文件，如果需要选择新的文件，请先删除之前的文件!");
                    },
                    handleExceedImg: function (files, fileList) {
                        this.$message.warning("当前只允许选择一个图片，如果需要选择新的图片，请先删除之前的图片!");
                    },
                    bigShow: function (item) {
                        // layer.open({
                        //     type: 1,
                        //     title: false,
                        //     closeBtn: 0,
                        //     area: ['500px', '500px'],
                        //     skin: 'layui-layer-nobg', //没有背景色
                        //     shadeClose: true,
                        //     content: '<img src="' + item + '" style="max-width:100%;height:100%">'
                        // });

                        // self._openTplFull({
                        //     tag: 'gwai500_img',
                        //     tagData: item,
                        //     index: 1,
                        //     openOpts: {
                        //         title: '图片预览',
                        //         area: ['500px','500px']
                        //         // maxWidth: '100px',
                        //         //maxHeight: '100px',
                        //     }
                        // });
                        /*var oldImg;

                        var fullCon = document.getElementById('imgFull');
                        fullCon.style.display = 'block';


                        var fullImg = document.getElementById('imgFull_box');
                        if(fullImg.childNodes.length === 1) {
                            oldImg = document.getElementById('fullImg');
                            fullImg.removeChild(oldImg)
                        }
                        var imgUrl = document.createElement('img');
                        imgUrl.src = item;
                        imgUrl.id = 'fullImg';
                        imgUrl.style.maxHeight = '500px';
                        imgUrl.style.margin = 'auto';
                        imgUrl.style.display = 'block';
                        fullImg.appendChild(imgUrl);*/
                        new Viewer(document.getElementById('GWAM015_IMGS'));
                    },
                    /*tranform1: function () { //左旋转
                        var img = document.getElementById('fullImg');
                        var width = img.width;
                        var height = img.height;

                        current = (current+90)%360;
                        img.style.transform = 'rotate('+current+'deg)';
                        // img.width = height;
                        // img.height = width;
                    },
                    tranform2: function () {//右旋转
                        var img = document.getElementById('fullImg');
                        var width = img.width;
                        var height = img.height;
                        current = (current-90)%360;
                        img.style.transform = 'rotate('+current+'deg)';
                        // img.width = height;
                        // img.height = width;
                    },
                    close: function () {
                        var fullCon = document.getElementById('imgFull');
                        fullCon.style.display = 'none';
                    },*/
                    beforeUploadVideo: function (file) {
                        const isLt10M = file.size / 1024 / 1024  < 10;
                        if (['video/mp4', 'video/ogg', 'video/flv','video/avi','video/wmv','video/rmvb'].indexOf(file.type) == -1) {
                            this.$message.warning('请上传正确的视频格式');
                            return false;
                        }
                        if (!isLt10M) {
                            this.$message.warning('上传视频大小不能超过10MB哦!');
                            return false;
                        }
                    },
                    uploadVideoProcess: function (event, file, fileList) {
                        vueData.videoFlag = true;
                        vueData.videoUploadPercent = file.percentage.toFixed(0);
                    },
                    handleVideoSuccess: function (res, file) {
                        vueData.videoFlag = false;
                        vueData.videoUploadPercent = 0;
                        if(res.code == 200){
                            vueData.videoForm.videoUploadId = res.data[0].name;
                            vueData.form.GWAM031 = res.data[0].name;
                            vueData.videoForm.Video = '/wissun-smfile/' +res.data[0].name;
                            if (pageVue.$refs.upload1.uploadFiles.length > 0) {
                                pageVue.$refs.upload1.submit();
                            } else {
                                if (vueData.isAdd) {
                                    self.add();
                                } else {
                                    self.edit();
                                }
                                pageVue.$refs.upload3.clearFiles();
                            }
                        }else{
                            this.$message.error('视频上传失败，请重新上传！');
                        }
                    }
                },
                created: function () {
                    self._setEleIsReady('gwai500_edit');
                }
            });
        },
        _lazy: function () {
            this._super();
        }
    });

    return new Gwai500Edit();

});
